#include "composite.h"

#include <iostream>

using namespace std;

Equipment::Equipment(const char* pName, const double price)
{
	m_sName = pName;
	m_dPrice = price;
}

Equipment::~Equipment()
{
	list<Equipment *>::iterator it;
	it = m_equipList.begin();
	while (it != m_equipList.end())
	{
		delete (*it);
		m_equipList.erase(it);
		it = m_equipList.begin();
	}
}

void Equipment::show(std::string format)
{
	cout<<m_sName;
}

double Equipment::price()
{
	return m_dPrice;
}

bool Equipment::add(Equipment* pEquip)
{
	return false;
}

bool Equipment::remove(Equipment* pEquip)
{
	return false;
}

void CompositeEquipment::show(std::string format)
{
	cout<<m_sName<<endl;

	format += "\t";

	list<Equipment *>::const_iterator it;
	for (it = m_equipList.begin();
		it != m_equipList.end(); ++it)
	{
		cout<<format;
		(*it)->show(format);
		cout<<endl;
	}
}

double CompositeEquipment::price()
{
	double totalPrice = m_dPrice;

	list<Equipment *>::const_iterator it;
	for (it = m_equipList.begin();
		it != m_equipList.end(); ++it)
	{
		totalPrice += (*it)->price();
	}

	return totalPrice;
}

bool CompositeEquipment::add(Equipment* pEquip)
{
	if (pEquip)
	{
		m_equipList.push_back(pEquip);
		return true;
	}
	else
	{
		return false;
	}
}

bool CompositeEquipment::remove(Equipment* pEquip)
{
	if (pEquip)
	{
		m_equipList.remove(pEquip);
		return true;
	}
	else
	{
		return false;
	}
}
